package com.jucai.pppeiyouclient.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.widget.Filter;
import android.widget.Filterable;

import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.base.CommonRecyclerViewAdapter;
import com.jucai.pppeiyouclient.bean.CityBean;
import com.jucai.pppeiyouclient.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Kuky on 2017-04-26.
 */

public class CityAdapter extends CommonRecyclerViewAdapter<CityBean> implements Filterable {
    private static final String TAG = "CityAdapter : ";
    private List<CityBean> storeCities;
    private CityFilter cityFilter;

    public CityAdapter(Context context, List<CityBean> data) {
        super(context, data);
    }

    @Override
    public void setData(List<CityBean> data) {
        super.setData(data);
        storeCities = data;
    }

    @Override
    public Filter getFilter() {
        if (cityFilter == null) {
            cityFilter = new CityFilter();
        }
        return cityFilter;
    }

    @Override
    public void bindHolder(CommonHolder holder, CityBean cityBean) {
        holder.setText(R.id.city_text, cityBean.getArea_name());
    }

    @Override
    protected int getItemType(int position) {
        return 0;
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.item_city_layout;
    }

    public class CityFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            FilterResults results = new FilterResults();
            List<CityBean> tempList;
            if (TextUtils.isEmpty(charSequence)) {
                tempList = storeCities;
            } else {
                tempList = new ArrayList<>();
                for (CityBean c : storeCities) {
                    if (c.getArea_name().contains(charSequence)) {
                        tempList.add(c);
                    }
                }
            }
            results.values = tempList;
            results.count = tempList.size();
            return results;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            mData = (List<CityBean>) filterResults.values;
            LogUtils.e(TAG + "mData = " + mData);
            if (filterResults.count > 0) {
                notifyDataSetChanged();
            }
        }
    }

}
